/**
 * Copyright (c) 2018 苍穹跃龙
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package com.l.web.master.system.controller;


import java.util.Arrays;
import java.util.List;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.Errors;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.l.core.web.RestCode;
import com.l.core.web.RestJson;
import com.l.web.master.system.entity.SysMenu;
import com.l.web.master.system.service.ISysMenuService;

/**
 * <p>
 * 菜单表 前端控制器
 * </p>
 *
 * @author 严秋旺
 * @since 2018-09-27
 */
@Controller
@RequestMapping("/master/system/menu")
public class SysMenuController {

    @Autowired
    private ISysMenuService menuService;

    @RequestMapping({"/", "index"})
    public String index(Model model) {
        return "master/system/menu/index";
    }

    @RequestMapping("list")
    @ResponseBody
    public RestJson list(SysMenu sysMenu) {
        List<SysMenu> list = menuService.findAllMenu();
        return RestJson.Success().put("list", list);
    }

    @RequestMapping("detail")
    @ResponseBody
    public RestJson detail(String id) {
        SysMenu sysMenu = menuService.selectById(id);
        return RestJson.Success().put("entity", sysMenu);
    }

    @RequestMapping(path = "save", method = RequestMethod.POST)
    @ResponseBody
    public RestJson save(@Valid SysMenu sysMenu, Errors errors) {
        if (errors.hasErrors()) {
            return new RestJson(RestCode.FORM_BAD, errors);
        }

        if (sysMenu.getId() == null) {
            menuService.insert(sysMenu);
        } else {
            menuService.updateById(sysMenu);
        }

        return RestJson.Success().put("id", sysMenu.getId());
    }

    @RequestMapping("remove")
    @ResponseBody
    public RestJson remove(Integer[] id) {
        if (id == null || id.length == 0) {
            return new RestJson(RestCode.FAILED.getCode(), "请设置删除id");
        }

        List<Integer> list = Arrays.asList(id);
        menuService.deleteBatchIds(list);
        return RestJson.Success();
    }
}
